package cn.com.guage.monitor.log.hikair.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;

import cn.com.guage.monitor.log.hikair.domain.Good;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags = { "数据库连接池数据本地写入测试" })
@RestController
public class HikariDemoController {
    
//    private final static Logger logger = LoggerFactory.getLogger("Application_ERROR");
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @ApiOperation("GET方式请求链路")
    @GetMapping("/demo1")
    public String selectDemo1DataFromDB() {
        logger.info("开始执行selectGoodsDataFromDB方法---------------------");
      //  int a =1/0;
        Good good = new Good();
        good.setGoodName("苹果手机");
        logger.info("商品名称为："+good.getGoodName());
        good.setAppraiseNum(1000);
        logger.info("评价数量为："+good.getAppraiseNum());
        good.setGoodsDesc("Iphone 8 puls,256G");
        logger.info("商品描述为："+good.getGoodsDesc());
        good.setGoodsStatus("未出售");
        logger.info("出售状态为："+good.getGoodsStatus());
        good.setPrice(5999);
        good.setSaleNum(100);
        good.setSaleTime(new Date());
        good.setVisitNum(402);
       /* for(int i=0;i<20;i++) {
        	logger.info("-----------------链路日志测试test"+i);
        }*/
        logger.info("查询到商品信息为："+JSON.toJSONString(good));
        return "trace-log-demo1返回信息为："+JSON.toJSONString(good);
    }
    
    @ApiOperation("GET方式请求链路异常抛出")
    @GetMapping("/demo2")
    public String exceptionTest() {
        logger.info("开始执行selectGoodsDataFromDB方法---------------------");
        try {
            logger.info("开始执行异常抛出代码---------------------");
			int a =1/0;
		} catch (Exception e) {
			logger.error("计算异常",e);
		}
        for(int i=0;i<20;i++) {
        	logger.info("-----------------异常链路日志测试test"+i);
        }
        logger.info("异常模拟测试结束");
        return "异常日志测试";
    }
 
    @ApiOperation("POST方式请求链路")
	@PostMapping("/demo2")
	public ResponseEntity<Good> demo2(@RequestBody Good good) {
		return ResponseEntity.ok().body(good);
	}
    
    @ApiOperation("get方式获取数据库连接池信息")
    @GetMapping("/demo3")
   	public String demo3() {    	
    	//hikariMonitor.getHikariMonitor();
   		return "ok";
   	}
    
    @GetMapping("/demo4")
    public  String printError() {
    	System.out.println("开始执行：printError");
    	try {
			int a = 1/0;
		} catch (Exception e) {
			System.out.println("输出1"+e);
			System.out.println("输出2"+e.getMessage());

			logger.error("执行异常1：",e.getMessage());
			logger.error("执行异常2：",e);
			logger.error("执行异常3："+e);
		}
    	return "OK";
    }
}